package util;

import entity.TableRecord;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
    private static SessionFactory sessionFactory = null;

    static {
        try {
            Configuration configure = new AnnotationConfiguration().addAnnotatedClass(TableRecord.class).configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                    .applySettings(configure.getProperties()).buildServiceRegistry();
            sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
            throw new RuntimeException(e);
       }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}
